package com.eascs.banknet.boc.dao;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Repository;

import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.TableData;
import com.eascs.banknet.boc.vo.bocS01.BOCS01Bean;
import com.eascs.banknet.common.dao.BaseO2oDao;
import com.eascs.common.util.StringUtil;

@Repository("bocS01Dao")
public class BOCS01Dao extends BaseO2oDao{
	@Inject
	protected JdbcSession crmJdbcSession;
	public BOCS01Bean queryData(String startId){
		//获取向怡亚通月均采购额
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT t1.loan_id 'customerId', t2.bank_applyno 'APPNO'")
			.append(" FROM o2o_loanapply t1, o2o_loan_user t2 ")
			.append(" WHERE t1.loanapply_no = t2.loanapply_no and (t1.data_source is null or t1.data_source!='2') ")
//			.append(" AND customer_salesamount IS NOT NULL")
			.append(" AND creditline IS NULL")
			.append(" AND t2.bank_status IS NOT NULL AND t2.spouse_id is null ")
			.append(" AND creditrange IS NULL AND t1.bank_type='BC'")
			.append(" AND t1.islock ='N' AND t2.bank_status = '01B3'")
			.append(" AND t1.wf_type in('N') ");
//			.append(" AND ea_purchaseamount is not null ");
		if(StringUtil.hasText(startId)){
			sql.append("   AND t2.bank_applyno > '").append(startId).append("' ");
		};
		sql.append(" ORDER BY t2.bank_applyno LIMIT 1");
		//从crm中查询门店行业类别
		BOCS01Bean bean =  super.findEntity(sql.toString(), BOCS01Bean.class);
		if(bean != null ){
			StringBuffer customerQuery = new StringBuffer();
			customerQuery.append("SELECT csi.vocation ,ROUND(csi.eapurchaseamount, 2) as REVENUE FROM customer_basic_info cbi, customer_shop_info csi")
				.append(" WHERE cbi.customer_id = csi.customer_id")
				.append(" AND csi.shoptype='MAN'")
				.append(" AND cbi.customer_id = ?")
				.append(" AND vocation is not null");
			List<Object[]> resluts=crmJdbcSession.findObjectsList(customerQuery.toString(), bean.getCustomerId());
			if (resluts.size()>0) {
				bean.setVOCATION(String.valueOf(resluts.get(0)[0]));
				bean.setREVENUE(String.valueOf(resluts.get(0)[1]));
			}
//			String vocation = crmJdbcSession.findString(customerQuery.toString(), bean.getCustomerId());
			
		}
		return bean;
	};
	
	public int updateInfo(String appno, String creditline, String creditrange){
		String loanapplyNo = super.findString("select loanapply_no from o2o_loan_user where bank_applyno = ?", appno);
		String sql = " UPDATE o2o_loanapply set creditline=?, creditrange=? where loanapply_no = ?";
		return super.execute(sql, creditline, creditrange, loanapplyNo);
	}
}
